/*
	The MIT License

	Copyright (c) 2009 Guilherme Almeida and Mauro de Tarso

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
	
	http://code.google.com/p/textanim/
	http://flupie.net
*/       

package flupie.textanim;


/**
 * Separate blocks of text in line with the specified breakMode
 *	
 *	@private
 */	
class Breaker
{
	public static var BREAK_IN_CHARS:String = "breakInChars";
	public static var BREAK_IN_WORDS:String = "breakInWords";
	public static var BREAK_IN_LINES:String = "breakInLines";
	
	public static function separeBlocks(block:TextAnim, ?breakMode:String):TextAnimBlock 
	{
		var firstBlock:TextAnimBlock;
		switch (breakMode) {
			case BREAK_IN_CHARS :
				firstBlock = breakInLetters(block);
			case BREAK_IN_WORDS :
				firstBlock = breakInWords(block);
			case BREAK_IN_LINES :
				firstBlock = breakInLines(block);
			default : firstBlock = breakInLetters(block);
		}
		return firstBlock;
	}
	
	public static function createBlock(anim:TextAnim, str:String, index:Int):TextAnimBlock 
	{
		var block:TextAnimBlock = new TextAnimBlock(anim, index);
		block.text = str;
		return block;
	}
	
	public static function breakInLetters(anim:TextAnim):TextAnimBlock 
	{
		var txt:String = anim.source.text;
		var block:TextAnimBlock = null;
		var firstBlock:TextAnimBlock = null;
		
		for (i in 0... txt.length) {
			var str:String = txt.substr(i, 1);
			if(str != String.fromCharCode(13)) {
				if (stringTest(str)) {
					var newBlock:TextAnimBlock = createBlock(anim, str, i);	
					if (block != null) {
						block.nextBlock = newBlock;
					} else {
						firstBlock = newBlock;
					}
					block = newBlock;
				}
			}
		}
		
		return firstBlock;
	}
	
	public static function breakInWords(anim:TextAnim):TextAnimBlock 
	{
		var str:String = "";
		var firstIndex:Int = 0;
		var txt:String = anim.source.text;
		var block:TextAnimBlock = null;
		var firstBlock:TextAnimBlock = null;
		var newBlock:TextAnimBlock;
		
		for (i in 0... txt.length) {
			var s:String = txt.substr(i, 1);
			
			if (s != " " && s != null && s != String.fromCharCode(13)) {
				if (str == "") firstIndex = i;
				str += s;
			} else if (s != null) {
				if (stringTest(str)) {
					newBlock = createBlock(anim, str, firstIndex);	
					if (block != null) {
						block.nextBlock = newBlock;
					} else {
						firstBlock = newBlock;
					}
					block = newBlock;
				}
				str = "";
			}
		}

		if (stringTest(str)) {
			newBlock = createBlock(anim, str, firstIndex);	
			if (block != null) {
				block.nextBlock = newBlock;
			} else {
				firstBlock = newBlock;
			}
			block = newBlock;
		}
		
		return firstBlock;
	}
	
	public static function breakInLines(anim:TextAnim):TextAnimBlock
	{
		var block:TextAnimBlock = null;
		var firstBlock:TextAnimBlock = null;
		
		for (i in 0... anim.source.numLines) {
				var str:String = anim.source.getLineText(i);
				if (str != "" && str.length > 0 && str != null) {
					if (stringTest(str)) {
						var newBlock:TextAnimBlock = createBlock(anim, str, anim.source.getLineOffset(i));	
						if (block != null) {
							block.nextBlock = newBlock;
						} else {
							firstBlock = newBlock;
						}
						block = newBlock;
					}
				}
		}
		return firstBlock;
	}
	
	public static function stringTest(str:String):Bool
	{
		if (str != "" && str != " " && str.length > 0) return true;
		
		return false;
	}
}
